﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Runtime.InteropServices;

namespace Presentation
{
    [DefaultEvent("Click")]
    public partial class UsecontrolMenu : UserControl
    {
        public UsecontrolMenu()
        {
            InitializeComponent();
            _bEnter = false;
            _bClick = false;
        }
        /// <summary>
        /// tạo biến để lấy hình ảnh
        /// </summary>
        protected Bitmap _myPic; //
        [Browsable(true)]
        public Bitmap MyPic
        {
            get { return _myPic; }
            set { _myPic = value; }
        }
        /// <summary>
        /// lấy chuỗi
        /// </summary>
        protected String _myText;
        public String MyText
        {
            get { return _myText; }
            set { _myText = value; }
        }
        protected bool _bEnter; //Kiem tra chuot nam trong usercontrol
        protected bool _bClick; //Kiem tra su kien click vao usercontrol
        /// <summary>
        /// Vẽ hình chữ nhật và tô màu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Paint(object sender, PaintEventArgs e)
        {
            
            Graphics g = e.Graphics;
            if (_bClick)
            {
                Color _c3 = Color.FromArgb(100, 100, 110, 250);
                Pen _pen = new Pen(_c3, 1);
                g.DrawRectangle(_pen, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
                Color _c4 = Color.FromArgb(100, 150, 190, 250);
              Brush b = new SolidBrush(_c4);
              g.FillRectangle(b, 0, 0, this.Width - 1, this.Height - 1);
            }
            else if (_bEnter)
            {
                
                    Color _c1 = Color.FromArgb(100, 150, 190, 250);
                    Pen _pen = new Pen(_c1, 1);
                    g.DrawRectangle(_pen, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
                    Color _c2 = Color.FromArgb(100, 210, 230, 250);
                    Brush b = new SolidBrush(_c2);
                    g.FillRectangle(b, 0, 0, this.Width - 1, this.Height - 1);
            } 
            if (_myPic != null)
            {
                int _width = Width*4/10;
                int _heigth = Height*4/10;
                int _x0 =(Width-_width)/2;
                int _y0 = (Height - _heigth)/2;  
                if (_bClick || _bEnter)
                {
                    _width = Width * 6 / 10;
                    _heigth = Height * 6 / 10;
                    _x0 = (Width-_width)/2;
                    _y0 = (Height - _heigth)/2;
                }
                g.DrawImage(_myPic, _x0, _y0, _width, _heigth);
            }
            Font _myFont = new Font("Arial", 10);
            Brush _myBrush = new SolidBrush(Color.DarkCyan);
            g.DrawString(_myText, _myFont, _myBrush, 23, 80);


        }

        private void Button_MouseEnter(object sender, EventArgs e)
        {
            _bEnter = true;
           if (!_bClick)
            { Invalidate(); }

        }

        private void Button_MouseHover(object sender, EventArgs e)
        {

        }

        private void Button_MouseLeave(object sender, EventArgs e)
        {
            _bEnter = false;
            if (!_bClick)
            { Invalidate(); }
        }

        private void Button_MouseClick(object sender, MouseEventArgs e)
        {
            if (!_bClick)
            {
                _bClick = true;
                Invalidate();
            }
        }

        public Boolean DaNhan
        {
            get { return _bClick; }
            set
            {
                _bClick = value;
                Invalidate();
            }
        }
    }
}
